package com.ce.webframework.sec.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;

import com.ce.webframework.sec.dao.ISecMsgTipsDao;
import com.ce.webframework.sec.model.SecMsgTipsDO;
import com.ce.webframework.sec.vo.SecMsgTipsVO;
import com.sitechasia.webx.core.dao.hibernate3.HibernateDomainObjectDao;

public class SecMsgTipsDaoImpl extends HibernateDomainObjectDao<SecMsgTipsDO> implements ISecMsgTipsDao {

	/**
	 * 功能描述:通过receiverCode查询消息集合
	 * @param receiverCode  接收者用户编码
	 * @return SecMsgTipsDO 集合
	 */
	public List<SecMsgTipsDO> getMsgTipsList(String receiverCode){
		System.out.println("receiverCode-------->"+receiverCode);
		StringBuffer hql = new StringBuffer();
		hql.append(" SELECT info FROM SecMsgTipsDO info ");
		hql.append(" WHERE info.receiverCode=? AND info.readFlag = '0' ");

		hql.append(" order by info.sendTime desc");
		Query query = getSession().createQuery(hql.toString());
		query.setParameter(0, receiverCode);
		List<SecMsgTipsDO> re = query.list();
		for(SecMsgTipsDO tip : re){
			tip.setReceiveTime(new Date());//消息接收后更新接收日期
//			tip.setReadFlag('5');
		}
//		getSession().flush();
		return  re;
	}

//	public List<SecMsgTipsDO> getMsgTipsByTimer(SecMsgTipsVO queryVO){
//		System.out.println("queryVO-------->"+queryVO);
//		StringBuffer hql = new StringBuffer();
//		hql.append(" SELECT info FROM SecMsgTipsDO info ");
//		hql.append(" WHERE info.receiverCode=? AND info.readFlag = '0' ");
//		hql.append(" AND info.remindTime = ? AND info.msgTipsId = ? ");
//		hql.append(" order by info.sendTime desc");
//		Query query = getSession().createQuery(hql.toString());
//		query.setParameter(0, queryVO.getReceiverCode());
//		query.setParameter(1, queryVO.getRemindTime());
//		query.setParameter(2, queryVO.getMsgTipId());
//		List<SecMsgTipsDO> re = query.list();
//
//		return  re;
//	}

	/**
	 * 功能描述:通过ids集合查询消息
	 * @param ids  消息纪录id主键集合
	 * @return SecMsgTipsDO 集合
	 */
	public List<SecMsgTipsDO> getMsgTipsByIds(List<Long> ids){
		StringBuffer hql = new StringBuffer();
		hql.append(" SELECT info FROM SecMsgTipsDO info WHERE info.msgTipId in (:ids) ");
		Query query = getSession().createQuery(hql.toString());
		query.setParameterList("ids", ids);
		List<SecMsgTipsDO> re = query.list();
		return  re;
	}

	/**
	 * 功能描述:新增消息
	 * @param secMsgTipsVO 持久化对象
	 */
	public void addMsgTips(SecMsgTipsDO secMsgTipsDO){
		getSession().persist(secMsgTipsDO);
	}

	/**
	 * 功能描述:系统启动时查询所有未读消息的receiverCode
	 * @return String receiverCode集合
	 */
	public List<String> getCodesNoReadMsg(){
		Query query = getSession().createQuery("select distinct info.receiverCode from SecMsgTipsDO info where info.readFlag = '0' order by info.sendTime desc");
		List codes = query.list();
		return codes;
	}

	/**
	 * 功能描述:更新消息提醒读标志
	 * @param ids  消息纪录id主键集合
	 * @param readFlag 读标志
	 * @return SecMsgTipsDO 集合
	 */
	public void updateReadFlag(List<Long> ids, Character readFlag){
		StringBuffer hql = new StringBuffer();
		hql.append(" UPDATE SecMsgTipsDO info SET info.readFlag = :readFlag ");
		hql.append(" WHERE info.msgTipId IN (:ids) ");
		Query query = getSession().createQuery(hql.toString());
		query.setParameterList("ids", ids);
		query.setParameter("readFlag", readFlag);
		query.executeUpdate();
	}

	/**
	 * 功能描述:查询今天需要初始化的定时器
	 * @return SecMsgTipsVO 集合
	 */
	public List<SecMsgTipsVO> getMsgTipForTimer(){
		StringBuffer hql = new StringBuffer();
		hql.append(" SELECT new com.ce.webframework.sec.vo.SecMsgTipsVO(info.msgTipId, info.remindTime, info.receiverCode) ");
		hql.append(" FROM SecMsgTipsDO info WHERE info.readFlag = '2' AND info.endTime > CURRENT_DATE() ");
		hql.append(" AND info.remindTime > CURRENT_DATE() AND info.remindTime < CURRENT_DATE()+1 ");
		Query query = getSession().createQuery(hql.toString());
//		query.setParameter("now", new Date());
		List gaps = query.list();
		return gaps;
	}

	/**
	 * 功能描述:删除消息纪录
	 * @param secMsgTipsDO
	 */
	public void deleteMsgTips(SecMsgTipsDO secMsgTipsDO){
		getSession().delete(secMsgTipsDO);
	}

	/**
	 * 功能描述:更新消息纪录
	 * @param secMsgTipsDO
	 */
	public void updateMsgTips(SecMsgTipsDO secMsgTipsDO){
		getSession().update(secMsgTipsDO);
	}
}
